package com.tubang.logistics.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.tubang.common.constant.Constants;
import com.tubang.common.util.BeanMapper;
import com.tubang.logistics.entity.GoodsEntity;
import com.tubang.logistics.entity.LogisticsOrderGoodsEntity;
import com.tubang.logistics.entity.PlatformOrderGoodsEntity;
import com.tubang.logistics.entity.TruckerOrderGoodsEntity;
import com.tubang.logistics.mapper.GoodsMapper;
import com.tubang.logistics.service.IGoodsService;
import com.tubang.logistics.service.ILogisticsOrderGoodsService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.tubang.logistics.service.IPlatformOrderGoodsService;
import com.tubang.logistics.service.ITruckerOrderGoodsService;
import com.tubang.logistics.vo.GoodsBaseVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 货品表 服务实现类
 * </p>
 *
 * @author wsl
 * @since 2017-12-31
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, GoodsEntity> implements IGoodsService {

    @Resource
    private IPlatformOrderGoodsService platformOrderGoodsService;
    @Resource
    private ILogisticsOrderGoodsService logisticsOrderGoodsService;
    @Resource
    private  ITruckerOrderGoodsService truckerOrderGoodsService;
    


    @Override
    public GoodsEntity selectBaseGoodsByOrderId(String orderId) {
        EntityWrapper<GoodsEntity>  entityWrapper = new EntityWrapper<GoodsEntity>();
        entityWrapper.eq("parent_id", Constants.DEFAULT_VAULE_ZERO);
        entityWrapper.eq("shipper_order_id",orderId);
        GoodsEntity goodsEntity = selectOne(entityWrapper);
        return goodsEntity;
    }

    @Override
    public GoodsEntity selectBaseGoodsByOrderId(String orderId, String status) {
        EntityWrapper<GoodsEntity>  entityWrapper = new EntityWrapper<GoodsEntity>();
        entityWrapper.eq("parent_id", Constants.DEFAULT_VAULE_ZERO);
        entityWrapper.eq("shipper_order_id",orderId);
      // entityWrapper.eq("goods_status",status);
        GoodsEntity goodsEntity = selectOne(entityWrapper);
        return goodsEntity;
    }

    @Override
    public List<GoodsEntity> selectGoodssByOrderId(String orderId) {
        EntityWrapper<GoodsEntity>  entityWrapper = new EntityWrapper<GoodsEntity>();
        entityWrapper.notIn("parent_id", Constants.DEFAULT_VAULE_ZERO);
        entityWrapper.eq("shipper_order_id",orderId);
        List<GoodsEntity> goodsEntities = selectList(entityWrapper);
        return goodsEntities;
    }

    @Override
    public List<GoodsEntity> selectGoodssByOrderId(String orderId, String status) {
        EntityWrapper<GoodsEntity>  entityWrapper = new EntityWrapper<GoodsEntity>();
        entityWrapper.eq("shipper_order_id",orderId);
        entityWrapper.eq("goods_status",status);
        entityWrapper.notIn("parent_id", Constants.DEFAULT_VAULE_ZERO);
        List<GoodsEntity> goodsEntities = selectList(entityWrapper);
        return goodsEntities;
    }


    @Override
    public List<GoodsEntity> selectGoodssByPlatFormOrderId(String orderId, String status) {
        List<PlatformOrderGoodsEntity> platformOrderGoodsEntities = platformOrderGoodsService.selectOrderByOrderId(orderId);
        List<GoodsEntity> goodsEntities=new ArrayList<GoodsEntity>();
        for (PlatformOrderGoodsEntity platformOrderGoodsEntity : platformOrderGoodsEntities) {
            GoodsEntity goodsEntity = selectById(platformOrderGoodsEntity.getGoodsId());
            if(goodsEntity==null)continue;
            goodsEntities.add(goodsEntity);
        }
        return goodsEntities;
    }


    @Override
    public GoodsEntity selectBaseGoodsByPlatFormOrderId(String orderId) {
        List<PlatformOrderGoodsEntity> platformOrderGoodsEntities = platformOrderGoodsService.selectOrderByOrderId(orderId);
        for (PlatformOrderGoodsEntity platformOrderGoodsEntity : platformOrderGoodsEntities) {
            GoodsEntity goodsEntity = selectById(platformOrderGoodsEntity.getGoodsId());
            if(goodsEntity==null)continue;
            if(goodsEntity.getParentId()==null||goodsEntity.getParentId().equals("0")){
                return goodsEntity;
            }else{
                GoodsEntity baseGoodsByGoodsId = selectBaseGoodsByGoodsId(goodsEntity.getId());
                return baseGoodsByGoodsId;
            }

        }
        return null;
    }


    @Override
    public boolean updateGoodssStatus(String goodsId, String status) {
        GoodsEntity goodsEntity = selectById(goodsId);
        goodsEntity.setGoodsStatus(status);
        boolean result = updateById(goodsEntity);
        return result;
    }

    @Override
    public GoodsEntity selectBaseGoodsByGoodsId(String goodsId) {
        GoodsEntity goodsEntity = selectById(goodsId);
        goodsEntity= selectBaseGoodsByOrderId(goodsEntity.getShipperOrderId());
        return goodsEntity;
    }

	@Override
	public List<GoodsEntity> selectGoodssByLogisticsOrderId(String orderId,Integer statusCode) {
		List<LogisticsOrderGoodsEntity> platformOrderGoodsEntities = logisticsOrderGoodsService.selectOrderByOrderId(orderId,statusCode);
        List<GoodsEntity> goodsEntities=new ArrayList<GoodsEntity>();
        for (LogisticsOrderGoodsEntity platformOrderGoodsEntity : platformOrderGoodsEntities) {
            GoodsEntity goodsEntity = selectById(platformOrderGoodsEntity.getGoodsId());
            if(goodsEntity==null||goodsEntity.getParentId()==null||goodsEntity.getParentId().equals("0"))continue;
            goodsEntities.add(goodsEntity);
        }
        return goodsEntities;
	}

    @Override
    public GoodsEntity selectBaseGoodsByLogisticsOrderId(String orderId) {
        List<LogisticsOrderGoodsEntity> logisticsOrderGoodsEntities = logisticsOrderGoodsService.selectOrderByOrderId(orderId,null);
        for (LogisticsOrderGoodsEntity logisticsOrderGoodsEntity : logisticsOrderGoodsEntities) {
            GoodsEntity goodsEntity = selectById(logisticsOrderGoodsEntity.getGoodsId());
            if(goodsEntity==null)continue;
            if(goodsEntity.getParentId()==null||goodsEntity.getParentId().equals("0")){
               return goodsEntity;
            }else{
                GoodsEntity baseGoodsByGoodsId = selectBaseGoodsByGoodsId(goodsEntity.getId());
                return baseGoodsByGoodsId;
            }

        }
        return null;
    }

    @Override
    public List<GoodsEntity> selectGoodssByTruckerOrderId(String orderId) {
        List<TruckerOrderGoodsEntity> truckerOrderGoodsEntities = truckerOrderGoodsService.selectOrderByOrderId(orderId);
        List<GoodsEntity> goodsEntities=new ArrayList<GoodsEntity>();
        for (TruckerOrderGoodsEntity truckerOrderGoodsEntity : truckerOrderGoodsEntities) {
            GoodsEntity goodsEntity = selectById(truckerOrderGoodsEntity.getGoodsId());
            if(goodsEntity==null||goodsEntity.getParentId()==null||goodsEntity.getParentId().equals("0"))continue;
            goodsEntities.add(goodsEntity);
        }
        return goodsEntities;
    }

    @Override
    public GoodsEntity selectBaseGoodsByTruckerOrderId(String orderId) {
        List<TruckerOrderGoodsEntity> truckerOrderGoodsEntities = truckerOrderGoodsService.selectOrderByOrderId(orderId);
        for (TruckerOrderGoodsEntity truckerOrderGoodsEntity : truckerOrderGoodsEntities) {
            GoodsEntity goodsEntity = selectById(truckerOrderGoodsEntity.getGoodsId());
            if(goodsEntity==null)continue;
            if(goodsEntity.getParentId()==null||goodsEntity.getParentId().equals("0")){
                return goodsEntity;
            }else{
                GoodsEntity baseGoodsByGoodsId = selectBaseGoodsByGoodsId(goodsEntity.getId());
                return baseGoodsByGoodsId;
            }

        }
        return null;
    }
}
